//index.js
//获取应用实例
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showMore: app.globalData.showMore,
    hasMessageNoRead: false,
    userInfo: {},
    activityInfo:{
      initiate:0,
      participate:0,
    },
    walletBalance:0,
    whetherSetPayPwd:'',
    isMsg:false,
    island:false,
    loginShow:false,
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  //跳转到钱包余额
  wallet:function(){
    app.TestBuriedPoint('yqlw_tabb_wallet','yqlw_tabb_wallet_pv')
    // app.TestBuriedPoint('yqlw_tabb_wallet_pv')
    if (!this.data.island) {
      this.landbtn()
    } else {
    wx.navigateTo({
      url: '/pages/mywallet/mywallet'
    })
    }
  },
  //更多消息
  msgshow(){
    var that = this
    if (!this.data.island) {
      this.landbtn()
    } else {
    that.setData({
      isMsg:true
    })
    }
  },
  cancelToExamine(){
    var that = this
    that.setData({
      isMsg: false
    })
  },
  //我的客服
  myService: function () {
    app.TestBuriedPoint('yqlw_tabb_aboutus_customerservice')
    var that = this
    that.setData({
      isMsg: false
    })
  },
  preventTouchMove: function (e) {
  },
  toggleToast(e) {
    console.log(e.detail)
    if (!e.detail.loginShow){
      this.toggle('loginShow', false)
    }
    this.onShow()
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    //我的页面埋点
    app.TestBuriedPoint('yqlw_home_tabb')
    wx.getSetting({
      success: (res) => {
        console.log('授权',res)
        if (wx.getStorageSync('uid')){
          console.log('已授权登陆')
          that.setData({
            island:true
          })
          that.getIspswd();
          //获取头像及名称
          that.getUserInfo();
          that.getWalletBalance();
          that.getActivityInitiate();
        }else{
            that.toggle("island", false);
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  
  //发起活动
  initiatingActivities:function() {
    app.TestBuriedPoint('yqlw_tabb_initiatingactivities')
    if(!this.data.island){
      this.landbtn()
    }else{
      wx.navigateTo({
        url: '/pages/myactivities/myactivities?type=1',
      });
    }
  },

  //参加活动
  participate: function(){
    app.TestBuriedPoint('yqlw_tabb_participate')
    if (!this.data.island) {
      this.landbtn()
    } else {
    wx.navigateTo({
      url: '/pages/myactivities/myactivities?type=1',
    });
    }
  },


  // 跳转到我的资料界面
  toMyInfo: function() {
    // app.TestBuriedPoint('yqlw_tabb_data','yqlw_tabb_data_pv')
    // app.TestBuriedPoint('yqlw_tabb_data_pv')
    getApp().mtj.trackEvent('yqlw_business_card');
    if (!this.data.island) {
      this.landbtn()
    } else {
    wx.navigateTo({
      url: '/pages/myinfo/myinfo'
    })
    }
  },
  // 跳转到个人主页页面
  toMyHomePage: function () {
    app.TestBuriedPoint('yqlw_tabb_homepage','yqlw_tabb_homepage_pv')
    // app.TestBuriedPoint('yqlw_tabb_homepage_pv')
    wx.navigateTo({
      url: '/pages/myhomepage/myhomepage'
    })
  },
  //跳转到我的红包
  toRedEnvelopes: function () {
    if (!this.data.island) {
      this.landbtn()
    } else {
    wx.navigateTo({
      url: '/pages/transactionRed/transactionRed'
    })
    }
  },

  // 跳转到意见反馈
  toHelp: function() {
    app.TestBuriedPoint('yqlw_tabb_feedback_pv','yqlw_tabb_feedback')
    // app.TestBuriedPoint('yqlw_tabb_feedback')
    wx.navigateTo({
      url: '/pages/helpmessage/helpmessage'
    })
  },

  // 跳转到关于我们
  toAbout: function() {
    app.TestBuriedPoint('yqlw_tabb_aboutus','yqlw_tabb_aboutus_pv')
    wx.navigateTo({
      url: '/pages/about/about'
    })
  },
  pswd(){
    if (!this.data.island) {
      this.landbtn()
    } else {
    if (this.data.whetherSetPayPwd){
      wx.navigateTo({
        url: '/pages/password/password?xg=' + 1
      })
    }else{
      wx.navigateTo({
        url: '/pages/password/password'
      })
    }
    }
  },
  
  //获取是否有密码
  getIspswd() {
    var that = this
    app.req({
      url: 'wechat/user/whetherSetPayPwd',
      data: {
      },
      method: 'POST',
      success: function (res) {
        console.log('密码', res);
        if (res.code == 200) {
          that.setData({
            whetherSetPayPwd: res.data.whetherSetPayPwd
          })
        }
      }
    })
  },

  getUserInfo: function () {
    var that = this;
    app.req({
      url: "wechat/user/getUserInformation",
      method: "post",
      data: {},
      success: function (res) {
        if (200 == res.code) {
          that.setData({
            userInfo: res.data
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    });
  },
  //获取活动发起列表
  getActivityInitiate: function(){
    let that = this
    app.req({
      url:"wechat/activity/actCount",
      method:"post",
      data:{},
      success: function (res) {
        if (200 == res.code) {
          that.setData({
            'activityInfo.initiate': res.data.createCount,
            'activityInfo.participate': res.data.partCount
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  //获取钱包余额
  getWalletBalance: function(){
    let that = this
    let data = {
      "pageNumber": 1,
      "pageSize": 10
    }
    app.req({
      url: "wechat/account/expenseCalendars",
      method: "post",
      data: data,
      success: function (res) {
        if(!res.data){
          res.code = 500
        }
        if (200 == res.code) {
          // console.log('余额：'+ JSON.stringify(res))
          var notReceivedBalance = res.data.notReceivedBalance || 0;
          var balance = res.data.balance || 0;
          var totalPrice = that.add(balance,notReceivedBalance);
          that.setData({
            walletBalance: totalPrice
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  sub(a,b){
    var that = this;
    var c, d, e;
  try {
    c = a.toString().split(".")[1].length;
  } catch (f) {
    c = 0;
  }
  try {
    d = b.toString().split(".")[1].length;
  } catch (f) {
    d = 0;
  }
  return e = Math.pow(10, Math.max(c, d)), (that.mul(a, e) - that.mul(b, e)) / e;

},
add(a,b){ 
  var that = this;
  var c, d, e;
try {
  c = a.toString().split(".")[1].length;
} catch (f) {
  c = 0;
}
try {
  d = b.toString().split(".")[1].length;
} catch (f) {
  d = 0;
}
return e = Math.pow(10, Math.max(c, d)), (that.mul(a, e) + that.mul(b, e)) / e;

  },
mul(a, b) {
  var c = 0,
    d = a.toString(),
    e = b.toString();
  try {
    c += d.split(".")[1].length;
  } catch (f) {}
  try {
    c += e.split(".")[1].length;
  } catch (f) {}
  return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
},
landbtn() {
    this.toggle('loginShow', true)
    this.selectComponent('#login').showClose()
},
  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function () {
    return {
      title: '您有一个活动邀请函，请查收！',
      path: '/pages/index/index',
    }
  },
})